//
//  WHYSocietyViewController.m
//  News163
//
//  Created by 王洪运 on 15/12/30.
//  Copyright © 2015年 王洪运. All rights reserved.
//

#import "WHYSocietyViewController.h"
#import "WHYSocietyCell.h"
#import "WHYSociety.h"
#import "WHYHTTPManager.h"
#import "SVProgressHUD.h"
#import "UIImageView+WebCache.h"

@interface WHYSocietyViewController ()

@property (strong, nonatomic) NSArray *societies;

@end

@implementation WHYSocietyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadSociety];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 加载数据
-(void)loadSociety {
    
    [SVProgressHUD show];
    
    [[WHYHTTPManager manager] loadNewsRequestWithNewsId:self.newsId finishedHandler:^(id result, NSError *error) {
        
        [SVProgressHUD dismiss];
        
        if (error) {
            [SVProgressHUD showErrorWithStatus:@"加载数据出错,请重试"];
            NSLog(@"%@",error);
            return;
        }
        
        self.societies = [WHYSociety societiesWithDictArray:result[self.newsId]];
        
        [self.tableView reloadData];
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.societies.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WHYSociety *socity = self.societies[indexPath.row];
    
    NSString *cellID = [WHYSocietyCell socirtyCellIdentifierWith:socity];
    
    WHYSocietyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.socity = socity;
    
    return cell;
}

#pragma mark - Table view delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WHYSociety *society = self.societies[indexPath.row];
    return [WHYSocietyCell socirtyCellHeightWith:society];
}

#pragma mark - 懒加载
-(NSArray *)societies {
    if (_societies == nil) {
        _societies = [NSMutableArray array];
    }
    return _societies;
}

@end
